package com.spdbcc;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * https://blog.csdn.net/JokerLJG/article/details/122693051
 */
public class java8 {

    List<Employee> emps = Arrays.asList(
            new Employee(101, "张三", 18, 9999.99),
            new Employee(102, "李四", 59, 6666.66),
            new Employee(103, "王五", 28, 3333.33),
            new Employee(104, "赵六", 8, 7777.77),
            new Employee(105, "田七", 38, 5555.55)
    );


    //需求：获取公司中年龄小于 35 的员工信息
    public List<Employee> filterEmployeeAge(List<Employee> emps){
        List<Employee> list = new ArrayList<>();

        for (Employee emp : emps) {
            if(emp.getAge() <= 35){
                list.add(emp);
            }
        }

        return list;
    }


    @Test
    public void test1(){
        List<Employee> list = filterEmployeeAge(emps);

        for (Employee employee : list) {
            System.out.println(employee);
        }
    }

    //需求：获取公司中工资大于 5000 的员工信息
    public List<Employee> filterEmployeeSalary(List<Employee> emps){
        List<Employee> list = new ArrayList<>();

        for (Employee emp : emps) {
            if(emp.getSalary() >= 5000){
                list.add(emp);
            }
        }

        return list;
    }


    @Test
    public void test2(){
        List<Employee> list = filterEmployeeSalary(emps);

        for (Employee employee : list) {
            System.out.println(employee);
        }
    }

    //优化方式一：策略设计模式

    /**
     *
     * @param emps
     * @param mp
     * @return
     */
    public List<Employee> filterEmployee(List<Employee> emps, MyPredicate<Employee> mp){
        List<Employee> list = new ArrayList<>();

        for (Employee employee : emps) {
            if(mp.test(employee)){
                list.add(employee);
            }
        }

        return list;
    }

    @Test
    public void test3() throws InterruptedException {
        List<Employee> list = filterEmployee(emps,new FilterEmployeeForAge());

        for (Employee employee : list) {
            System.out.println(employee);
        }

        System.out.println("------------------------------------------");
        Thread.currentThread().sleep(10);
        List<Employee> employeeList = filterEmployee(emps, new FilterEmployeeForSalary());
        for (Employee employee : employeeList) {
            System.out.println(employee);
        }
    }


    //优化方式二：匿名内部类
    @Test
    public void test4() throws InterruptedException {
        List<Employee> list = filterEmployee(emps, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getAge() > 18;
            }
        });

        list.forEach(System.out::println);

//        System.out.println("------------------------------------------");
//        Thread.currentThread().sleep(10);
//        List<Employee> employeeList = filterEmployee(emps, new FilterEmployeeForSalary());
//        for (Employee employee : employeeList) {
//            System.out.println(employee);
//        }
    }

    //优化方式三：Lambda 表达式

    @Test
    public void test5() {
        List<Employee> list = filterEmployee(emps, (e)->e.getAge() >18);
        list.forEach(System.out::println);
    }

    //优化方式四：Stream API

    @Test
    public void testStream() {
        List<Employee> list = emps.stream().filter((e)->e.getAge()>18).collect(Collectors.toList());
        list.forEach(System.out::println);
    }
}
